﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MaxLeaf.Common;
using System.Data.SqlClient;
using MaxLeaf.DB;
using System.Data;


namespace MaxLeaf.DAL.K2K
{
    public class t_ComBrandDAL : BaseDAL
    {
        /// <summary>
        /// 品牌
        /// </summary>
        public t_ComBrandDAL()
            : base()
        {
            strTableName = "t_ComBrand";
            strUpdateFNumberTableName = "t_ComBrand";

            strFieldNames = "FGrade,FBrandLevel,FStyle,FArea,FPriceRange,FBrandDistribution,FDepartment,FSupplier,FContactor,FPhone,FHomePage,FNote,FNumber,FName";

            strFItemClassID = Const.FItemClassID_Brand;
        }
        

        protected override void DAL_GetFieldData(DBAccess dbOrgK3Con, DBAccess dbDestK3Con, string strFieldName, DataRow drOrgData, DataRow drDestData)
        {
             if (strFieldName.Equals("FitemID", StringComparison.InvariantCultureIgnoreCase))
            {
                if (CvtFunc.ToInt(drDestData[strFieldName]) != 0) return;
                string[] arrFnumber = CvtFunc.ToString(drOrgData["Fnumber"]).Split('.');
                
                drDestData[strFieldName] = GetFItemID(dbOrgK3Con, dbDestK3Con, "0", "0", arrFnumber);

            }
            else
            {
                base.DAL_GetFieldData(dbOrgK3Con, dbDestK3Con, strFieldName, drOrgData, drDestData);
            }
        }

        private string GetFItemID(DBAccess dbOrgK3Con, DBAccess dbDestK3Con, string strOrgFParentID, string strFParentID, string[] arrFnumber)
        {
            
            const int intFDetail = 1;
            string strFItemId = string.Empty;
            t_ItemDAL dal = new t_ItemDAL(strFItemClassID, strUpdateFNumberTableName);



            strFItemId = dal.GetFItemId(dbOrgK3Con, dbDestK3Con, 1, strOrgFParentID, strFParentID, string.Join(".", arrFnumber, 0, arrFnumber.Length), intFDetail);

            UpdateAccess(dbDestK3Con, strFItemId, strFParentID);

            return strFItemId;
        }

        private string GetFParentID(DBAccess dbOrgK3Con, DBAccess dbDestK3Con, string[] arrFnumber)
        {
            
            const int intFDetail = 0;
            string strFItemId = string.Empty;
            t_ItemDAL dal = new t_ItemDAL(strFItemClassID, strUpdateFNumberTableName);
            for (int i = 0; i < arrFnumber.Length - 1; i++)
            {
                string strFParentID = "0";
                if (i != 0)
                {
                    strFParentID = strFItemId;
                }
                //得到每级的FitemID
                strFItemId = dal.GetFItemId(dbOrgK3Con, dbDestK3Con, i + 1, string.Empty, strFParentID, string.Join(".", arrFnumber, 0, i + 1), intFDetail);

                UpdateAccess(dbDestK3Con, strFItemId, strFParentID);
            }

            return strFItemId;
        }

        private void UpdateAccess(DBAccess dbDestK3Con, string strFItemId, string strFParentID)
        {
            string strSql = @"
                if not exists(select * from access_t_ComBrand where FitemID=@FitemID)
                begin
                 Insert into access_t_ComBrand(FitemID,FParentIDX,FDataAccessView,FDataAccessEdit,FDataAccessDelete)
                 Values(@FitemID,@FParentIDX,convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)))
                end
                ";
            dbDestK3Con.ExecuteSql(strSql, new SqlParameter[] { new SqlParameter("@FitemID", strFItemId), new SqlParameter("@FParentIDX", strFParentID) });
        }

         

        protected override DataTable DAL_GetData(DBAccess dbOrgK3Con, DBAccess dbDestK3Con, int intdataType, object objParam)
        {
            GetDataType dataType = (GetDataType)intdataType;

            if (dataType == GetDataType.OrgData)
            {
                return Gett_ComBrand(dbOrgK3Con, CvtFunc.ToString(objParam));
            }


            return base.DAL_GetData(dbOrgK3Con, dbDestK3Con, intdataType, objParam);
        }



        private DataTable Gett_ComBrand(DBAccess dbOrgK3Con, string strFItemID)
        {
            DataTable dtt_ComBrand = null;
            if (dtt_ComBrand == null)
            {
                dtt_ComBrand = dbOrgK3Con.GetTableData("t_ComBrand", new Dictionary<string, object> { { "FitemID", strFItemID } });

            }
            return dtt_ComBrand;
        }

    }
}